<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Exception;
use Illuminate\Support\Facades\Session;

class MemberController extends Controller {

        public function servicer_list(){
         if(Session::get('role_id') == 1 ) {
             $servicer = DB::table('servicer')
                 ->where('status',0)
                 ->select('id', 'name', 'face_url', 'telephone', 'nikename', 'worker_code')
                 ->get();
             $servicer = json_decode($servicer,1);

             foreach ($servicer as $key=>$value){
                 $servicer[$key]['face_url'] = '/face_url/'.$value['face_url'];
             }

         }else if(Session::get('role_id') == 2 ) {
             $servicer = DB::table('servicer')
                 ->where('company_id',Session::get('session_id'))
                 ->where('status',0)
                 ->select('id', 'name', 'face_url', 'telephone', 'nikename', 'worker_code')
                 ->get();

             $servicer = json_decode($servicer,1);

             foreach ($servicer as $key=>$value){
                 $servicer[$key]['face_url'] = '/face_url/'.$value['face_url'];
             }


         }
         return view('admin/member/servicer_list',['servicer'=>$servicer]);

        }

        /*
         * 业务员删除
         * */

        public function servicer_delete(Request $request){
                $bool = DB::table('servicer')->where('id',$request->id)->delete();
                if($bool){
                    echo "<script language='javascript'>alert('删除成功');window.location.href='/admin/member/servicer_list';</script>";
                }else{
                   echo "<script language='javascript'>alert('删除失败');window.location.href='/admin/member/servicer_list';</script>";
                }
        }



        /*
         * 添加业务员
         * */
        public function servicer_add(Request $request){
                $role_id = Session::get('role_id');
                if($role_id == 2){
                    return view('/admin/member/servicer_add');
                }else{
                    echo "<script language='javascript'>alert('进入失败，必须公司账号');window.location.href='/admin/supplyer/list';</script>";
                }
        }


        /*
         *添加业务员处理
         * */
        public function servicer_add_do(Request $request){

                foreach ($request->all() as $key=>$value){
                    if(empty($value)){
                        header('Content-Type:text/html;charset=utf-8');
                        echo "<script language='javascript'>alert('添加失败,不能有空');window.location.href='/admin/member/servicer_list';</script>";
                        exit;
                    }
                }
                if(isset($request->face_url)&&is_file($request->face_url)){
                   $face_url = $this->image('face_url',$request);
                }else{
                   $face_url = 'dezuo.png';
                }

              $data = $request->all();
              unset($data['_token']);
              unset($data['face_url']);
              $data['face_url'] = $face_url;
              $data['company_id'] = Session::get('session_id');

              try {
                   DB::table('servicer')->insert($data);
                  echo "<script language='javascript'>alert('添加成功');window.location.href='/admin/member/servicer_list';</script>";
              }catch (\Exception $e){
                   echo "<script language='javascript'>alert('添加失败，数据不能重复');window.location.href='/admin/member/servicer_list';</script>";
              }
        }


        public function driver_list(){

            if(Session::get('role_id') == 1 ) {
             $driver = DB::table('driver')
                 ->where('status',0)
                 ->select('id', 'name', 'face_url', 'telephone', 'nikename', 'car_photo','car_license','person_license')
                 ->get();
         }else if(Session::get('role_id') == 2 ) {
             $driver = DB::table('driver')
                 ->where('company_id',Session::get('session_id'))
                 ->where('status',0)
                 ->select('id', 'name', 'face_url', 'telephone', 'nikename', 'car_photo','car_license','person_license')
                 ->get();
         }

             $driver = json_decode($driver,1);
             foreach ($driver as $key=>$value){

                 $driver[$key]['face_url'] = '/face_url/'.$value['face_url'];
                 $driver[$key]['car_photo'] = '/image/'.$value['car_photo'];
                 $driver[$key]['car_license'] = '/image/'.$value['car_license'];
                 $driver[$key]['person_license'] = '/image/'.$value['person_license'];


             }

                return view('/admin/member/driver_list',['driver'=>$driver]);
        }


        /*
         * 司机添加
         * */
        public function driver_add(){
            $role_id = Session::get('role_id');
            if($role_id == 2){
                return view('/admin/member/driver_add');
            }else{
                echo "<script language='javascript'>alert('进入失败，必须公司账号');window.location.href='/admin/supplyer/list';</script>";
            }
        }

        /*
         * 司机添加处理
         * */
        public  function driver_add_do(Request $request){

                foreach ($request->all() as $key=>$value){
                    if(empty($value)){
                        header('Content-Type:text/html;charset=utf-8');
                        echo "<script language='javascript'>alert('添加失败,不能有空');window.location.href='/admin/member/driver_list';</script>";
                        exit;
                    }
                }
                if(isset($request->face_url)&&is_file($request->face_url)){
                    $face_url = $this->image('face_url',$request);
                }else{
                    $face_url = 'dezuo.png';
                }

              $data = $request->all();
              unset($data['_token']);
              unset($data['face_url']);
              $data['face_url'] = $face_url;
              $data['company_id'] = Session::get('session_id');
              $data['password'] = '$2y$10$ml3dbCVf1741xxDNG3X3BuoDLlxBqAizqRKFgseKouweha4VrJcxK';
              try {
                   DB::table('driver')->insert($data);
                   echo "<script language='javascript'>alert('添加成功');window.location.href='/admin/member/driver_list';</script>";
              }catch (\Exception $e){
                   echo "<script language='javascript'>alert('添加失败，数据不能重复');window.location.href='/admin/member/driver_list';</script>";
              }
        }




        /*
         *
         * 司机删除
         * */
        public function driver_delete(Request $request){
                $bool = DB::table('driver')->where('id',$request->id)->update(array('status'=>1));
                if($bool){
                    echo "<script language='javascript'>alert('删除成功');window.location.href='/admin/member/driver_list';</script>";
                }else{
                   echo "<script language='javascript'>alert('删除失败');window.location.href='/admin/member/driver_list';</script>";
                }
        }




        /*
         * 存头像
         * */
        public function image($field,$request){
            $file = $request->file($field);//接前台图片
            if($file->getmimeType() == "image/png" || $file->getmimeType() == "image/jpeg"){
                $extension = $file->extension();//图片路径
                $name = date('YmdHis',time()).rand(00000000,99999999999).'.'.$extension;
                $file->move("./face_url",$name);//移动至框架图片文件夹
                return $name;
            }
        }


}